package com.fjrcloud.service;

import com.fjrcloud.dao.ProjectGroupDao;
import com.fjrcloud.entity.ProjectGroup;
import com.fjrcloud.exception.BusinessException;
import com.fjrcloud.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * Created by shengjian.lin on 17-5-6.
 */
@Service
public class ProjectService {
    @Autowired
    private ProjectGroupDao dao;

    public Page<ProjectGroup> list(Integer pageNum, Integer pageSize) {
        pageNum = PageUtils.checkPageNum(pageNum);
        pageSize = PageUtils.checkPageSize(pageSize);
        PageRequest pageRequest = new PageRequest(pageNum, pageSize);

        return dao.findAllByValid(true,pageRequest);
    }

    public ProjectGroup get(Integer id) {
        return dao.findOne(id);
    }

    public void del(Integer id) throws BusinessException {
        ProjectGroup group = get(id);
        if (group == null)
            throw new BusinessException("更新对象不存在");

        group.setValid(false);

        dao.save(group);
    }

    public ProjectGroup save(ProjectGroup group) throws BusinessException {

        if (group.getId() != null){
            ProjectGroup old = get(group.getId());
            if (old == null)
                throw new BusinessException("更新对象不存在");
        }

        return dao.save(group);
    }

}
